package com.steel.yfmall.cloud.assistant.goods.impl;


import com.steel.yfmall.cloud.assistant.common.ChannelInfoService;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductSellerGoodsOtherBean;
import com.steel.yfmall.cloud.assistant.goods.ProductSellerGoodsService;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductSellerGoodsBean;
import com.steel.yfmall.cloud.assistant.dto.result.goods.ResultProductSellerGoodsBean;
import com.steel.yfmall.cloud.assistant.goods.mapper.ProductSellerGoodsMapper;
import com.steel.yfmall.cloud.assistant.po.goods.ProductSellerGoods;
import com.steel.yfmall.cloud.assistant.po.goods.ProductSellerGoodsExample;
import com.steel.yfmall.cloud.common.bean.param.APIParaDepotInfoBean;
import com.steel.yfmall.cloud.common.bean.result.APIResultDepotInfoBean;
import com.steel.yfmall.cloud.components.utils.base.Pages;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import com.steel.yfmall.cloud.components.utils.utils.UtilDate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author yuchaohua
 * @version V1.0
 * @Title: SteelProductSellGoodsServiceImpl
 * @Package com.steel.yfmall.cloud.product.service.impl
 * @Description: (用一句话描述该文件做什么)
 * @date 2020/11/21 16:24
 */
@Service
@Slf4j
public class ProductSellerGoodsServiceImpl implements ProductSellerGoodsService {

    @Resource
    private ProductSellerGoodsMapper productSellerGoodsMapper;

    @Resource
    private ChannelInfoService channelInfoService;

    /**
     * 统计符合条件的商品
     *
     * @return long    返回类型
     * @Title: countSellerGoodsListBySite
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @author yuchaohua
     * @date 2020/12/8 20:38
     */
    @Override
    public long countSellerGoodsListBySite(ParaProductSellerGoodsBean paraBean) {
        ProductSellerGoodsExample example = new ProductSellerGoodsExample();
        getSearchData(paraBean, example);
        return productSellerGoodsMapper.countByExample(example);
    }

    /**
     * 获取商品基础信息
     * 必传参数(站点，商品编码)
     *
     * @return com.steel.yfmall.cloud.product.po.ProductSellerGoodsBean    返回类型
     * @Title: getSellerGoodsBySite
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @author yuchaohua
     * @date 2020/11/21 17:10
     */
    @Override
    public ResultProductSellerGoodsBean getSellerGoodsBySite(ParaProductSellerGoodsBean paraBean) {
        ResultProductSellerGoodsBean returnBean = null;
        ProductSellerGoodsExample example = new ProductSellerGoodsExample();
        getSearchData(paraBean, example);
        ProductSellerGoods dbBean = productSellerGoodsMapper.selectOneByExampleSelective(example);
        if (null != dbBean) {
            returnBean = new ResultProductSellerGoodsBean();
            MobileBaseBeanUtils.copyAllFields(dbBean, returnBean);
            APIParaDepotInfoBean apiParaBean = new APIParaDepotInfoBean();
            apiParaBean.setDepotCode(returnBean.getDepotCode());
            APIResultDepotInfoBean depotInfoBean = channelInfoService.getDepotInfoBeanByPara(apiParaBean);
            // 仓库信息
            if (depotInfoBean != null) {
                returnBean.setDepotName(depotInfoBean.getDepotName());
                returnBean.setStartCode(depotInfoBean.getStartCode());
                returnBean.setDeliveryArea(depotInfoBean.getDeliveryArea());
                returnBean.setDeliveryAreaName(depotInfoBean.getDeliveryAreaName());
            }
        }
        return returnBean;
    }

    /**
     *  根据 16个字段查询商品编码
     * @param paraBean
     * @author liuzhijun
     * @date 2021/4/7 15:18
     * @return com.steel.yfmall.cloud.assistant.dto.result.goods.ResultProductSellerGoodsBean
     */
    @Override
    public ResultProductSellerGoodsBean getSellerGoodsProductSysCodeBySite(ParaProductSellerGoodsBean paraBean) {
        ResultProductSellerGoodsBean returnBean = null;
        ProductSellerGoodsExample example = new ProductSellerGoodsExample();
        getSearchDataProductSysCode(paraBean, example);
        ProductSellerGoods dbBean = productSellerGoodsMapper.selectOneByExampleSelective(example);
        if (null != dbBean) {
            returnBean = new ResultProductSellerGoodsBean();
            MobileBaseBeanUtils.copyAllFields(dbBean, returnBean);
        }
        return returnBean;
    }

    /**
     * 批量查询商品基础信息集合
     * 必传参数 站点编码 +  商品编码或商品编码集合
     *
     * @return java.util.List<com.steel.yfmall.cloud.product.po.ProductSellerGoodsBean>    返回类型
     * @Title: getSellerGoodsBySite
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @author yuchaohua
     * @date 2020/11/21 17:11
     */
    @Override
    public List<ResultProductSellerGoodsBean> getSellerGoodsListBySite(ParaProductSellerGoodsBean paraBean) {
        List<ResultProductSellerGoodsBean> returnListBean = null;
        if (null == paraBean) {
            return null;
        }
        ProductSellerGoodsExample example = new ProductSellerGoodsExample();
        getSearchData(paraBean, example);
        if (paraBean.getLength() > 0) {
            example.limit(paraBean.getStart(), paraBean.getLength());
        }
        List<ProductSellerGoods> dbListBean = productSellerGoodsMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(dbListBean)) {
            returnListBean = new ArrayList<>();
            Set<String> depotCodeList = new HashSet<>();
            for (ProductSellerGoods dbBean : dbListBean) {
                ResultProductSellerGoodsBean returnBean = new ResultProductSellerGoodsBean();
                MobileBaseBeanUtils.copyAllFields(dbBean, returnBean);
                // 缓存中获取仓库信息
                APIResultDepotInfoBean depotInfoBean = channelInfoService.getDepotInfoBeanRedisByPara(returnBean.getDepotCode());
                // 仓库信息
                if (depotInfoBean != null) {
                    returnBean.setDepotName(depotInfoBean.getDepotName());
                    returnBean.setStartCode(depotInfoBean.getStartCode());
                    returnBean.setDeliveryArea(depotInfoBean.getDeliveryArea());
                    returnBean.setDeliveryAreaName(depotInfoBean.getDeliveryAreaName());
                } else {
                    depotCodeList.add(returnBean.getDepotCode());
                }
                returnListBean.add(returnBean);
            }
            //  缓存中不足的仓库信息重新从服务中获取
            if (CollectionUtils.isNotEmpty(depotCodeList)) {
                APIParaDepotInfoBean apiParaBean = new APIParaDepotInfoBean();
                apiParaBean.setDepotCodeList(new ArrayList<>(depotCodeList));
                HashMap<String, APIResultDepotInfoBean> depotInfoBeanHashMap = channelInfoService.getDepotInfoBeanMapByPara(apiParaBean);
                if (depotInfoBeanHashMap != null) {
                    for (ResultProductSellerGoodsBean returnBean : returnListBean) {
                        if (depotInfoBeanHashMap != null && depotInfoBeanHashMap.containsKey(returnBean.getDepotCode())) {
                            APIResultDepotInfoBean depotInfoBean = depotInfoBeanHashMap.get(returnBean.getDepotCode());
                            returnBean.setDepotName(depotInfoBean.getDepotName());
                            returnBean.setStartCode(depotInfoBean.getStartCode());
                            returnBean.setDeliveryArea(depotInfoBean.getDeliveryArea());
                            returnBean.setDeliveryAreaName(depotInfoBean.getDeliveryAreaName());
                        }
                    }
                }
            }

        }
        return returnListBean;
    }


    /**
     * 获取店铺商品集合(key : siteCode + goodsSn)
     * 必传参数 站点编码 +  商品编码或商品编码集合
     *
     * @param paraBean
     * @return
     */
    @Override
    public Map<String, ResultProductSellerGoodsBean> getProductSellerGoodsBeanMap(ParaProductSellerGoodsBean paraBean) {
        Map<String, ResultProductSellerGoodsBean> returnMapBean = null;
        List<ResultProductSellerGoodsBean> resultListBean = getSellerGoodsListBySite(paraBean);
        if (CollectionUtils.isNotEmpty(resultListBean)) {
            returnMapBean = new HashMap<>();
            for (ResultProductSellerGoodsBean itemBean : resultListBean) {
                String goodsKey = itemBean.getSiteCode() + itemBean.getProductSysCode();
                returnMapBean.put(goodsKey, itemBean);
            }
        }
        return returnMapBean;
    }

    /**
     * 修改商品价格信息
     *
     * @return boolean    返回类型
     * @Title: updateProductSellerGoods
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @Param recordBean 参数说明
     * @author yuchaohua
     * @date 2020/12/9 0:56
     */
    @Override
    public boolean updateProductSellerGoods(ParaProductSellerGoodsBean paraBean, ProductSellerGoods recordBean) {
        if (null == paraBean || null == recordBean) {
            return false;
        }
        if (StringUtils.isBlank(paraBean.getProductSysCode()) && CollectionUtils.isEmpty(paraBean.getProductSysCodeList())) {
            return false;
        }
        recordBean.setUpdateTime(UtilDate.getDate());
        recordBean.setRelUpdateDate(UtilDate.getDate());
        ProductSellerGoodsExample example = new ProductSellerGoodsExample();
        getSearchData(paraBean, example);
        return productSellerGoodsMapper.updateByExampleSelective(recordBean, example) > 0;
    }

    @Override
    public long count(ParaProductSellerGoodsOtherBean paraBean) {
        ProductSellerGoodsExample example = new ProductSellerGoodsExample();
        ProductSellerGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(paraBean.getStatus());
        criteria.andProdClassEqualTo(paraBean.getProdClass());
        return productSellerGoodsMapper.countByExample(example);
    }

    /**
     * 分页查询 定时任务专用
     *
     * @author jia chang bao
     */
    @Override
    public Pages<ProductSellerGoods> getProductSellerGoods(ParaProductSellerGoodsOtherBean paraBean, long count) {
        if (count > 0) {
            Pages<ProductSellerGoods> productSellerGoodsPages = new Pages<>();
            ProductSellerGoodsExample productSellerGoodsExample = new ProductSellerGoodsExample();
            ProductSellerGoodsExample.Criteria criteria = productSellerGoodsExample.createCriteria();
            // 商品销售模式：1 普通现货商品  2 定制商品
            //已上架的
            criteria.andStatusEqualTo(paraBean.getStatus());
            //钢材
            criteria.andProdClassEqualTo(paraBean.getProdClass());
            List<ProductSellerGoods> productSellerGoods = productSellerGoodsMapper.selectByExample(productSellerGoodsExample);
            if (!CollectionUtils.isEmpty(productSellerGoods)) {
                productSellerGoodsPages.setList(productSellerGoods);
                return productSellerGoodsPages;
            }
        }
        return null;
    }

    /**
     * 入参拼接
     *
     * @param paraBean
     * @param example
     */
    public void getSearchData(ParaProductSellerGoodsBean paraBean, ProductSellerGoodsExample example) {
        if (null == example || null == paraBean) {
            return;
        }
        if (StringUtils.isNotBlank(paraBean.getOrderByValue())) {
            example.setOrderByClause(paraBean.getOrderByValue());
        }
        ProductSellerGoodsExample.Criteria criteria = example.createCriteria();
        // 站点编码
        if (StringUtils.isNotBlank(paraBean.getSiteCode())) {
            criteria.andSiteCodeEqualTo(paraBean.getSiteCode());
        }
        // 平台商品编码
        if (StringUtils.isNotBlank(paraBean.getProductSysCode())) {
            criteria.andProductSysCodeEqualTo(paraBean.getProductSysCode());
        }
        if (CollectionUtils.isNotEmpty(paraBean.getProductSysCodeList())) {
            criteria.andProductSysCodeIn(paraBean.getProductSysCodeList());
        }
//        // 商品用途别
//        if (StringUtils.isNotBlank(paraBean.getProductApnno())) {
//            criteria.andProductApnnoEqualTo(paraBean.getProductApnno());
//        }
//        // 商品用途别
//        if (StringUtils.isNotBlank(paraBean.getProductTradeno())) {
//            criteria.andProductApnnoEqualTo(paraBean.getProductTradeno());
//        }
        // 产品形态
        if (StringUtils.isNotBlank(paraBean.getProductTypeNo())) {
            criteria.andProductTypeNoEqualTo(paraBean.getProductTypeNo());
        }
        // 仓库编码
        if (StringUtils.isNotBlank(paraBean.getDepotCode())) {
            criteria.andDepotCodeEqualTo(paraBean.getDepotCode());
        }
        // 状态: -1为删除，0为禁用，1为上架，2为待售
        if (paraBean.getStatus() != null) {
            criteria.andStatusEqualTo(paraBean.getStatus());
        }
        // 商品销售模式：1 普通现货商品  2 定制商品
        if (paraBean.getSalesMode() != null) {
            criteria.andSalesModeEqualTo(paraBean.getSalesMode());
        }
        // 商品价格纬度  1 组合纬度价格  2副产品定价模式
        if (paraBean.getProductPriceMode() != null) {
            criteria.andProductPriceModeEqualTo(paraBean.getProductPriceMode());
        }
        //产品大类   H为钢材，B为钢坯，C为科技砖，F为副产品，M为模铸，K为矿产品，S为溶剂产品，Z为其他
        if (paraBean.getProdClass() != null) {
            criteria.andProdClassEqualTo(paraBean.getProdClass());
        }
    }


    public void getSearchDataProductSysCode(ParaProductSellerGoodsBean paraBean, ProductSellerGoodsExample example) {
        if (null == example || null == paraBean) {
            return;
        }
        ProductSellerGoodsExample.Criteria criteria = example.createCriteria();
        // 产品形态
        if (StringUtils.isNotBlank(paraBean.getProductTypeNo())) {
            criteria.andProductTypeNoEqualTo(paraBean.getProductTypeNo());
        }
        if(paraBean.getBrandId()!=null){
            criteria.andBrandIdEqualTo(paraBean.getBrandId());
        }
        // 仓库编码
        if (StringUtils.isNotBlank(paraBean.getDepotCode())) {
            criteria.andDepotCodeEqualTo(paraBean.getDepotCode());
        }
        //产品大类   H为钢材，B为钢坯，C为科技砖，F为副产品，M为模铸，K为矿产品，S为溶剂产品，Z为其他
        if (paraBean.getProdClass() != null) {
            criteria.andProdClassEqualTo(paraBean.getProdClass());
        }
        if(paraBean.getProductThick()!=null){
            criteria.andProductThickEqualTo(BigDecimal.valueOf(paraBean.getProductThick()));
        }
        if(paraBean.getProductWidth()!=null){
            criteria.andProductWidthEqualTo(paraBean.getProductWidth());
        }
        if(paraBean.getProductLen()!=null){
            criteria.andProductLenEqualTo(paraBean.getProductLen());
        }
        if(paraBean.getCraft()!=null){
            criteria.andCraftEqualTo(paraBean.getCraft());
        }
        if(StringUtils.isNotBlank(paraBean.getColor())){
            criteria.andColorEqualTo(paraBean.getColor());
        }
        if(StringUtils.isNotBlank(paraBean.getSizeInformation())){
            criteria.andSizeInformationEqualTo(paraBean.getSizeInformation());
        }
        if(StringUtils.isNotBlank(paraBean.getCountWgtMode())){
            criteria.andCountWgtModeEqualTo(paraBean.getCountWgtMode());
        }
        if(StringUtils.isNotBlank(paraBean.getTransWayNo())){
            criteria.andTransWayNoEqualTo(paraBean.getTransWayNo());
        }
        if(StringUtils.isNotBlank(paraBean.getProductGrade())){
            criteria.andProductGradeEqualTo(paraBean.getProductGrade());
        }
        if(StringUtils.isNotBlank(paraBean.getProductTradeno())){
            criteria.andProductTradenoEqualTo(paraBean.getProductTradeno());
        }
        if(StringUtils.isNotBlank(paraBean.getIsDowngrade())){
            criteria.andIsDowngradeEqualTo(paraBean.getIsDowngrade());
        }
        if(paraBean.getSizePieces()!=null){
            criteria.andSizePiecesEqualTo(paraBean.getSizePieces());
        }

    }


}
